from django.http import HttpResponse
from datetime import datetime
from django.utils.timezone import make_aware
def index(request):
    response = HttpResponse("ok")
    expires = datetime(year=2019,month=5,day=29,hour=10,minute=30,second=0)
    expires = make_aware(expires)
    response.set_cookie('user_id','91wangfan',max_age=180,path="/cms/")
    #path在这里是对指定的路径有效
    return response

def delete_cookie(request):
    response = HttpResponse("delete")
    response.delete_cookie('user_id')
    return response

def my_list(request): #通过 http://127.0.0.1:8001/list/ 来获取cookie
    #如果设置了 path="/cms/" 那么 它获取的就是none
    cookies = request.COOKIES
    username = cookies.get('user_id')
    return HttpResponse(username)

def cms_list(request): # 这个就可以获取
    cookies = request.COOKIES
    username = cookies.get('user_id')
    return HttpResponse(username)

def session_view(request):
    # request.session['username']="daxiong"
    # username = request.session.get('username')
    # username = request.session.pop('username')
    #只是获取到了  但是 session 值 已经没了
    request.session['username'] = "daxiong"
    request.session['userid'] = 10
    # print(request.session.get('userid'))
    # request.session.clear()
    # request.session.flush()
    # request.session.set_expiry(None)
    # request.session.set_expiry(-1)
    request.session.set_expiry(0)
    request.session.clear_expired() #清除过期的session
    # request.session.set_expiry(0)
    print(request.session.get('userid'))
    return HttpResponse("ok")

